package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeExpertAdviceService;
import com.bidevalution.dao.entity.BeExpertAdvice;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.util.OConvertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 标段报表控制层
 *
 * @author 查慧英
 * @date 2019-11-20 17:45
 */
@RestController
@RequestMapping("/beAdvice")
@Api(description = "专家独立评审意见")
public class BeExpertAdviceController extends BaseController {
    private Logger logger = Logger.getLogger(getClass());
    private static final String SECTION_ID = "sectionId";
    private static final String SEQ_ID = "seqId";
    private static final String EXPERT_ID = "expertId";
    private static final String UNPASSED_INFO = "unpassedInfo";
    private static final String OBJECTION_INFO = "objectionInfo";
    private static final String USER_GUID = "userGuid";
    private static final String LOGIN_NAME = "loginName";
    @Autowired(required = false)
    private BeExpertAdviceService beExpertAdviceService;
    /**
     * 获取专家废标说明
     *  @param request
     * @param response
     */
    @ApiOperation(value = "获取专家废标说明", notes = "获取专家废标说明")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/getUnPassedInfo")
    public Response<BeExpertAdvice> getUnPassedInfo(HttpServletRequest request, HttpServletResponse response) {
        setHttpServletResponse(response);
        try {
            // 获取请求中的参数
            String sectionId = request.getParameter(SECTION_ID);
            String userGuid = request.getHeader(USER_GUID);
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                Map<String, Object> params = new HashMap<>(4);
                params.put(SECTION_ID, sectionId);
                params.put(LOGIN_NAME, tsUser.getLoginName());
                return beExpertAdviceService.getUnPassedInfo(params);
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
    /**
     * 获取有异议的意见和理由
     *  @param request
     * @param response
     */
    @ApiOperation(value = "获取有异议的意见和理由", notes = "获取有异议的意见和理由")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/getObjectionInfo")
    public Response<BeExpertAdvice> getObjectionInfo(HttpServletRequest request, HttpServletResponse response) {
        setHttpServletResponse(response);
        try {
            // 获取请求中的参数
            String sectionId = request.getParameter(SECTION_ID);
            String userGuid = request.getHeader(USER_GUID);
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                Map<String, Object> params = new HashMap<>(4);
                params.put(SECTION_ID, sectionId);
                params.put(LOGIN_NAME, tsUser.getLoginName());
                return beExpertAdviceService.getObjectionInfo(params);
            } else {
                return Response.fail("获取专家账号登录信息失败");
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
    /**
     * 新增修改专家评审意见
     *  @param request
     * @param response
     */
    @ApiOperation(value = "新增修改专家评审意见", notes = "新增修改专家评审意见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = SECTION_ID, value = "标段Id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = SEQ_ID, value = "主键", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = EXPERT_ID, value = "专家id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = UNPASSED_INFO, value = "废标情况说明", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = OBJECTION_INFO, value = "有异议的意见和理由", paramType = "query", dataType = "String")
    })
    @PostMapping("/addOrUpdate")
    public Response addOrUpdate(HttpServletRequest request, HttpServletResponse response) {
        setHttpServletResponse(response);
        try {
            // 获取请求中的参数
            BeExpertAdvice item = new BeExpertAdvice();
            item.setExpertId(request.getParameter(EXPERT_ID));
            String objectionInfo = request.getParameter(OBJECTION_INFO);
            if (OConvertUtils.isNotEmpty(objectionInfo)) {
                item.setObjectionInfo(request.getParameter(OBJECTION_INFO));
            }
            item.setSectionId(request.getParameter(SECTION_ID));
            item.setSeqId(request.getParameter(SEQ_ID));
            String unpassedInfo = request.getParameter(UNPASSED_INFO);
            if (OConvertUtils.isNotEmpty(unpassedInfo)) {
                item.setUnpassedInfo(request.getParameter(UNPASSED_INFO));
            }
            return beExpertAdviceService.addOrUpdate(item);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
}
